import request from '../common/request'
import store from '../store'
// 获取分类
export const getIndexNav = function () {
	// console.log(request);
	return request({
		url: '/postclass',
		method: 'get'
	})
}

// /postclass/'+id+'/post/'+page 获取列表得数据 id为分类的id page为第几页
export const getIndexList = function (id, page) {
	console.log(id, page);
	// console.log(request);
	return request({
		url: `/postclass/${id}/post/${page}`,
		method: 'get', token: true
	})
}

// 获取详情页 post/id
export const getdetail = function (id) {
	return request({
		url: `/post/${id}`,
		method: 'get'
	})
}

// 搜索 search/post  POST keyword: "测试" page 搜索内容post  搜索话题topic
export const getSearch = function (data) {
	console.log(data.type);
	return request({
		url: `/search/${data.type}`,
		method: 'post',
		data: {
			keyword: data.keyword,
			page: data.page,
		},
		token: true
	})
}

// 话题的详情页
// /topic/'+this.info.id+'/post/'+page
export const getTopicDetail = function (id, page) {
	return request({
		url: `/topic/${id}/post/${page}`,
		method: 'get',
		token: true
	})
}

// 账号密码登录 url = '/user/login'  username password post

export const userLogin = function (data) {
	return request({
		url: `/user/login`,
		method: 'post', data
	})
}

// 手机号登录 /user/phonelogin  	phone, code

export const phoneLogin = function (data) {
	return request({
		url: `/user/phonelogin `,
		method: 'post', data
	})
}
// 获取验证码/user/sendcode phone
export const sendcode = function (phone) {
	return request({
		url: `/user/sendcode`,
		method: 'post', data: { phone }
	})
}

// 第三方登录 /user/otherlogin provider openid expires_in nickName avatarUrl
export const otherlogin = function (data) {
	return request({
		url: `/user/otherlogin`,
		method: 'post', data
	})
}

// 修改手机号 /user/bindphone phone code
export const bindphone = function (data) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/user/bindphone`,
		method: 'post', data, token: true
	})
}
// 设置or修改密码 oldpassword:this.oldpassword,newpassword:this.newpassword,renewpassword:this.renewpassword,
export const editPwd = function (data) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/repassword`,
		method: 'post', data, token: true
	})
}
// 绑定邮箱 /user/bindemail email token
export const bindemail = function (data) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/user/bindemail`,
		method: 'post', data, token: true
	})
}

// 修改资料

export const edituserinfo = function (data) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/edituserinfo`,
		method: 'post', data, token: true
	})
}

// 关注
export const follow = function (data) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/follow`,
		method: 'post', data, token: true
	})
}

// 点赞  post_id:this.item.id, type:type === 'support' ? 0 : 1
export const support = function (data) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/support`,
		method: 'post', data, token: true
	})
}


// 发布文章接口 POST /post/create
/*
isopen自己可见传0 全部可见1
text:文章内容
topic_id：话题id
post_class_id 分类id
imglist：//传入通过JSON处理过的图片id数组	
*/

export const createArticle = function (data) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/post/create`,
		method: 'post', data, token: true, default: true
	})
}

// 获取个人空间的详情 /getuserinfo  user_id
export const getuserinfo = function (data) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/getuserinfo`,
		method: 'post', data, token: true,
	})
}

// 获取用户相关信息 /user/getcounts/'+this.user_id
export const getcounts = function (id) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/user/getcounts/${id}`,
		method: 'get', token: true,
	})
}
// 获取用户的详情页的文章 '/user/'+this.user_id+'/post/'+page

export const userList = function (user_id, page) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/user/${user_id}/post/${page}`,
		method: 'get', token: true,
	})
}

// 取消关注  unfollow
export const unfollow = function (data) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/unfollow`,
		method: 'post', data, token: true
	})
}


// 关注列表 followpost/page
export const followpost = function (page) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: `/followpost/${page}`,
		method: 'get', token: true
	})
}
// 获取文章的评论 post/1359/comment get

export const comment = function (id) {
	return request({
		url: `/post/${id}/comment`,
		method: 'get'
	})
}
// 发布评论 /post/comment
export const postComment = function (data) {
	if (!store.state.token) {
		return uni.showToast({
			title: "无效token",
			icon: "none",
		});
	}
	return request({
		url: "/post/comment",
		method: 'post',
		token: true,
		data
	})
}